package com.thinklib.service.impl;

import com.thinklib.bean.Book;
import com.thinklib.bean.ExtCode;
import com.thinklib.mapper.FileUploadMapper;
import com.thinklib.service.FileUploadService;
import com.thinklib.util.FileSize2B;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.UUID;

@Service
public class FileUploadServiceImpl implements FileUploadService {
	
	@Resource
	private FileUploadMapper fileUploadMapper;

	@Value("${file.upload.url}")
	private String uploadFilePath;

	@Value("${img.upload.url}")
	private String uploadImgPath;

	@Resource
	private Book book;

	@Resource
	private ExtCode extCode;

	@Resource
	private FileUploadService fileUploadService;

	@Override
	public int addFile(Book book) {
		int aid = fileUploadMapper.addFile(book);
		return aid;
	}

	@Override
	public int addExtCode(ExtCode extCode) {
		int effectLine = fileUploadMapper.addExtCode(extCode);
		return effectLine;
	}

	@Override
	public String upload(HttpServletRequest request) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		MultipartFile avatar = fileMap.get("avatar");
		MultipartFile file = fileMap.get("file");
		String alibaba_cloud = request.getParameter("alibaba_cloud");
		String alibaba_ext_code = request.getParameter("alibaba_ext_code");
		String baidu_cloud = request.getParameter("baidu_cloud");
		String baidu_ext_code = request.getParameter("baidu_ext_code");

		// 获取文件名
		String originalFilename = file.getOriginalFilename();
		String avatarName = avatar.getOriginalFilename();
		// 获取文件大小
		long size = file.getSize();
		// 获取文件名称
		String fileName = originalFilename.substring(0, originalFilename.lastIndexOf("."));
		// 获取文件后缀名
		String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
		String avatarSuffix = avatarName.substring(avatarName.lastIndexOf("."));
		// 生成随机文件名
		UUID uuid = UUID.randomUUID();
		// 去掉UUID的横杠
		String newFilename = Long.toHexString(uuid.getMostSignificantBits())
				+ Long.toHexString(uuid.getLeastSignificantBits());
		// 获取当前时间
		long currentDate = System.currentTimeMillis();
		// 格式化当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String currentDateStr = sdf.format(currentDate);
		// 创建当前时间文件夹
		File currentDateFile = new File(uploadFilePath);
		File currentDateImg = new File(uploadImgPath + File.separator + currentDateStr);
		// 判断当前时间文件夹是否存在
		if (!currentDateFile.exists()) {
			currentDateFile.getParentFile().mkdirs();
		}
		if (!currentDateImg.exists()) {
			currentDateImg.getParentFile().mkdirs();
		}

		// 拼接文件路径
		String fileUrl = currentDateFile + File.separator + fileName + fileSuffix;
		String logoUrl = currentDateImg + File.separator + newFilename + avatarSuffix;

		File fileDest = new File(fileUrl);
		File logoDest = new File(logoUrl);

		if (!fileDest.exists() || !fileDest.isDirectory()) {
			fileDest.getParentFile().mkdirs();
		}
		if (!logoDest.exists() || !logoDest.isDirectory()) {
			logoDest.getParentFile().mkdirs();
		}
		try {
			file.transferTo(fileDest);
			avatar.transferTo(logoDest);

			book.setBookName(fileName);
			book.setAlibabaCloud(alibaba_cloud);
			book.setBaiduCloud(baidu_cloud);
			String newSize = FileSize2B.getNetFileSizeDescription(size);
			book.setFileSize(newSize);
			book.setAvatar("/avatar/" + currentDateStr + File.separator + newFilename + avatarSuffix);
			int bookId = fileUploadService.addFile(book);

			// 插入提取码
			extCode.setBookId(bookId);
			extCode.setAlibabaCloudExtCode(alibaba_ext_code);
			extCode.setBaiduCloudExtCode(baidu_ext_code);
			int i = fileUploadService.addExtCode(extCode);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "上传成功";
	}

}
